package gui.panels;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import mysql.LocationQueries;
import mysql.MoviesQueries;
import mysql.MusicQueries;
import objects.Location;
import objects.Music;

public class AddMusic extends JFrame implements ActionListener {
	
	public Music mcurrentEntry;
	public MusicQueries mmusicQueries;
	public List<Music> mResults;
	public int mnumberOfEntries = 0;
	public int mCurrentEntryIndex = 0;
	
	public Location loccurrentEntry;
	public LocationQueries locQueries;
	public List<Location> locResults;
	public int locnumberOfEntries = 0;
	public int locCurrentEntryIndex = 0;

	private JLabel artistLabel;
	public JTextField artistText;
	private JLabel albumLabel;
	public JTextField albumText;
	private JLabel locLabel;
	public JComboBox locCombo;
	public JButton add;
	public JButton cancel;
	public JPanel displayPanel;
	public int nextEntry;

	public AddMusic() {
		setData();
		getLocations();
		artistLabel = new JLabel("Album:");
		artistText = new JTextField(20);
		albumLabel = new JLabel("Artist:");
		albumText = new JTextField(20);
		locLabel = new JLabel("Location id");
		displayPanel = new JPanel();
		add = new JButton("Add new music");
		cancel = new JButton("Cancel");

		displayPanel.setLayout(new GridLayout(4, 2, 4, 4));
		displayPanel.add(artistLabel);
		displayPanel.add(artistText);
		displayPanel.add(albumLabel);
		displayPanel.add(albumText);
		displayPanel.add(locLabel);
		displayPanel.add(locCombo);
		displayPanel.add(add);
		displayPanel.add(cancel);

		add.addActionListener(this);
		cancel.addActionListener(this);

		add(displayPanel, BorderLayout.CENTER);
		pack();
		setVisible(true);
	}

	private void setData() {
		mmusicQueries = new MusicQueries();
		mResults = mmusicQueries.getAllMusic();
		nextEntry = mResults.get(mResults.size() - 1).getMuid() + 1;
	}
	
	private void getLocations() {
		locQueries = new LocationQueries();
		locResults = locQueries.getAllLocations();
		String[] comboArray = new String[locResults.size()];
		for (int i=0;i<locResults.size();i++) {
			comboArray[i] = locResults.get(i).getAddress();
		}
		locCombo = new JComboBox(comboArray);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == this.add) {
			mmusicQueries.addMusic(nextEntry, artistText.getText(), albumText.getText(), locCombo.getSelectedIndex()+101, 0);
			super.dispose();
		}
		if (arg0.getSource() == this.cancel) {
			super.dispose();
		}
	}

}
